import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:myapp/splash_page.dart';

import 'app_cache.dart';
import 'home/home_page.dart';
import 'login/login_page.dart';

class MainRouter {
  static getPages() {
    return [
      GetPage(name: '/', page: () => const SplashPage()),
      GetPage(name: '/login', page: () => LoginPage()),
      GetPage(
          name: '/home',
          page: () => HomePage(),
          middlewares: [TokenMiddleware()]),
    ];
  }
}

class TokenMiddleware extends GetMiddleware {
  @override
  RouteSettings? redirect(String? route) {
    if (AppCache.get().getToken().isEmpty) {
      return const RouteSettings(name: '/login');
    }
    return null;
  }
}

class TokenController extends GetxController {

  void handleTokenInvalid() {
    AppCache.get().saveTokenAndRole("", "");
    Get.offAllNamed('/login');
  }

}
